/*
*  session.c - Session management functions
*  Copyright (c) 2005 Derek Ingrouville (dergro@users.sourceforge.net)
*
*  This program is free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU Library General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program; if not, write to the Free Software
*  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

#include "session.h"

MsnSession *
msn_session_new ()
{
	MsnSession *session = NULL;

	session = g_new (MsnSession, 1);
	session->notif = msn_notification_new (session);
	session->acct = msn_account_new ();
	session->sbsessions = NULL;
	session->clist = NULL;
	session->notif->connected = FALSE;

	return session;
}

void
msn_session_disconnect (MsnSession *session)
{
	GList *list = NULL;

	g_return_if_fail (session != NULL);

	if (!session->notif->connected)
		return;

	msn_notification_disconnect (session->notif);

	list = session->sbsessions;
	while (list != NULL) {
		MsnSwitchboard *sboard = NULL;
		sboard = (MsnSwitchboard *) list->data;
		if (sboard == NULL)
			break;
		msn_switchboard_free (sboard);
		list = g_list_next (list);
	}

	session->connected = FALSE;

	return;
}

void
msn_session_free (MsnSession *session)
{
	g_return_if_fail (session != NULL);

	if (session->connected)
		msn_session_disconnect (session);

	if (session->notif != NULL) {
		msn_notification_free (session->notif);
		session->notif = NULL;
	}

	if (session->clist != NULL) {
		msn_clist_free (session->clist);
		session->clist = NULL;
	}

	if (session->acct != NULL) {
		msn_account_free (session->acct);
		session->acct = NULL;
	}

	if (session->sbsessions != NULL) {
		g_list_free (session->sbsessions);
		session->sbsessions = NULL;
	}

	g_free (session);

	return;
}

int
msn_session_connect (MsnSession *session, const char *server, unsigned int port)
{     
	int ret;

	g_return_val_if_fail (session != NULL, -1);
	g_return_val_if_fail (server != NULL, -1);
	g_return_val_if_fail (port > 0, -1);

	if (session->notif->connected)
		msn_session_disconnect (session);

	ret = msn_notification_connect (session->notif, server, port);
	if (ret == 0)
		session->notif->connected = TRUE;

	return ret;
}

void
msn_session_add_switchboard (MsnSession *session, MsnSwitchboard *sboard)
{
	g_return_if_fail (session != NULL);
	g_return_if_fail (sboard != NULL);

	session->sbsessions = g_list_append (session->sbsessions, sboard);
}

MsnSwitchboard *
msn_session_find_switchboard_from_id (MsnSession *session, int id)
{
	MsnSwitchboard *sboard = NULL;
	GList *list = NULL;

	g_return_val_if_fail (session != NULL, NULL);

	list = g_list_first (session->sbsessions);

	while (list != NULL) {
		MsnSwitchboard *tmp = (MsnSwitchboard *) list->data;

		if (tmp == NULL)
			break;

		if (tmp->id == id) {
			sboard = tmp;
			break;
		}

		list = g_list_next (list);
	}

	return sboard;
}

void
msn_session_remove_switchboard (MsnSession *session, MsnSwitchboard *sboard)
{
	g_return_if_fail (session != NULL);
	g_return_if_fail (sboard != NULL);

	session->sbsessions = g_list_remove (session->sbsessions, sboard);
}
